package com.greensam.pixelengine.controller;

import com.greensam.pixelengine.annotation.AuthCheck;
import com.greensam.pixelengine.facade.SpaceFacadeService;
import com.greensam.pixelengine.facade.UserFacadeService;
import com.greensam.pixelengine.pojo.base.BaseResponse;
import com.greensam.pixelengine.pojo.base.DeleteRequest;
import com.greensam.pixelengine.pojo.base.PaginationResponse;
import com.greensam.pixelengine.pojo.emuns.RoleEnum;
import com.greensam.pixelengine.pojo.entity.UserPo;
import com.greensam.pixelengine.pojo.vo.space.request.SpaceCreateRequest;
import com.greensam.pixelengine.pojo.vo.space.request.SpaceEditRequest;
import com.greensam.pixelengine.pojo.vo.space.request.SpacePageRequest;
import com.greensam.pixelengine.pojo.vo.space.request.SpaceUpdateRequest;
import com.greensam.pixelengine.pojo.vo.space.response.SpaceLevelVo;
import com.greensam.pixelengine.pojo.vo.space.response.SpaceVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Ma Chengrui
 * @since 2025/9/12 16:36
 */
@Tag(name = "SpaceController", description = "空间模块")
@RestController
@RequestMapping("/space")
@RequiredArgsConstructor
public class SpaceController {

    private final UserFacadeService userFacadeService;
    private final SpaceFacadeService spaceFacadeService;

    @Operation(summary = "创建空间")
    @PostMapping("/create")
    public BaseResponse<Long> createSpace(HttpServletRequest httpServletRequest, @RequestBody SpaceCreateRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceFacadeService.createSpace(request, loginUser.getId()));
    }

    @Operation(summary = "删除空间")
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteSpace(HttpServletRequest httpServletRequest, @RequestBody @Valid DeleteRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceFacadeService.deleteSpace(request, loginUser.getId()));
    }

    @Operation(summary = "编辑空间")
    @PostMapping("/edit")
    public BaseResponse<Boolean> editSpace(HttpServletRequest httpServletRequest, @RequestBody @Valid SpaceEditRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceFacadeService.editSpace(request, loginUser.getId()));
    }

    @Operation(summary = "获取空间详情")
    @GetMapping("/get")
    public BaseResponse<SpaceVo> getSpaceDetail(@RequestParam Long id) {
        return BaseResponse.success(spaceFacadeService.getSpaceDetail(id));
    }

    @Operation(summary = "空间管理 - 分页查询空间")
    @PostMapping("/manager/page")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<PaginationResponse<SpaceVo>> pageSpace(@RequestBody @Valid SpacePageRequest request) {
        return BaseResponse.success(spaceFacadeService.pageSpace(request));
    }

    @Operation(summary = "空间管理 - 更新空间")
    @PostMapping("/manager/update")
    @AuthCheck(mustRole = {RoleEnum.ADMIN})
    public BaseResponse<Boolean> updateSpace(HttpServletRequest httpServletRequest, @RequestBody @Valid SpaceUpdateRequest request) {
        UserPo loginUser = userFacadeService.getLoginUser(httpServletRequest);
        return BaseResponse.success(spaceFacadeService.updateSpace(request, loginUser.getId()));
    }

    @Operation(summary = "获取空间等级")
    @GetMapping("/list/level")
    public BaseResponse<List<SpaceLevelVo>> listSpaceLevel() {
        return BaseResponse.success(spaceFacadeService.listSpaceLevel());
    }

}
